<html>
<head>
  <meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=ISO-8859-1">
  <title>oaosvm.m</title>
<link rel="stylesheet" type="text/css" href="../../m-syntax.css">
</head>
<body>
<code>
<span class=defun_kw>function</span>&nbsp;<span class=defun_out>model&nbsp;</span>=&nbsp;<span class=defun_name>oaosvm</span>(<span class=defun_in>data,options</span>)<br>
<span class=h1>%&nbsp;OAOSVM&nbsp;Multi-class&nbsp;SVM&nbsp;using&nbsp;One-Against-One&nbsp;decomposition.</span><br>
<span class=help>%&nbsp;</span><br>
<span class=help>%&nbsp;<span class=help_field>Synopsis:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;oaosvm(&nbsp;data&nbsp;)</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;oaosvm(&nbsp;data,&nbsp;options&nbsp;)</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Description:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;oaosvm(&nbsp;data&nbsp;)&nbsp;uses&nbsp;one-agains-one&nbsp;deconposition</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;to&nbsp;train&nbsp;the&nbsp;multi-class&nbsp;Support&nbsp;Vector&nbsp;Machines&nbsp;(SVM)</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;classifier.&nbsp;The&nbsp;classification&nbsp;into&nbsp;nclass&nbsp;classes&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;is&nbsp;decomposed&nbsp;into&nbsp;nrule&nbsp;=&nbsp;(nclass-1)*nclass/2&nbsp;binary&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;problems.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;oaosvm(&nbsp;data,&nbsp;options)&nbsp;allows&nbsp;to&nbsp;specify&nbsp;the</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;binary&nbsp;SVM&nbsp;solver&nbsp;and&nbsp;its&nbsp;paramaters.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Input:</span></span><br>
<span class=help>%&nbsp;&nbsp;data&nbsp;[struct]&nbsp;Training&nbsp;data:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.X&nbsp;[dim&nbsp;x&nbsp;num_data]&nbsp;Training&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.y&nbsp;[1&nbsp;x&nbsp;num_data]&nbsp;Labels&nbsp;of&nbsp;training&nbsp;data&nbsp;(1,2,...,nclass).&nbsp;</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;options&nbsp;[struct]&nbsp;Control&nbsp;parameters:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.bin_svm&nbsp;[string]&nbsp;Function&nbsp;which&nbsp;implements&nbsp;the&nbsp;binary&nbsp;SVM&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;solver;&nbsp;(default&nbsp;'smo').</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.verb&nbsp;[1x1]&nbsp;If&nbsp;1&nbsp;then&nbsp;a&nbsp;progress&nbsp;info&nbsp;is&nbsp;displayed&nbsp;(default&nbsp;0).</span><br>
<span class=help>%&nbsp;&nbsp;The&nbsp;other&nbsp;fields&nbsp;of&nbsp;options&nbsp;specifies&nbsp;the&nbsp;options&nbsp;of&nbsp;the&nbsp;binary</span><br>
<span class=help>%&nbsp;&nbsp;solver&nbsp;(e.g.,&nbsp;ker,&nbsp;arg,&nbsp;C).&nbsp;See&nbsp;help&nbsp;of&nbsp;the&nbsp;selected&nbsp;solver.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Output:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;[struct]&nbsp;Multi-class&nbsp;SVM&nbsp;majority&nbsp;voting&nbsp;classifier:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Alpha&nbsp;[nsv&nbsp;x&nbsp;nrule]&nbsp;Weights&nbsp;(Lagrangeans).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.bin_y&nbsp;[2&nbsp;x&nbsp;nrule]&nbsp;Translation&nbsp;between&nbsp;binary&nbsp;responses&nbsp;of</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;the&nbsp;discriminant&nbsp;functions&nbsp;and&nbsp;class&nbsp;labels.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.b&nbsp;[nrule&nbsp;x&nbsp;1]&nbsp;Biases&nbsp;of&nbsp;discriminant&nbsp;functions.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.sv.X&nbsp;[dim&nbsp;x&nbsp;nsv]&nbsp;Support&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.nsv&nbsp;[1x1]&nbsp;Number&nbsp;of&nbsp;support&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.trnerr&nbsp;[1x1]&nbsp;Training&nbsp;error.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.kercnt&nbsp;[1x1]&nbsp;Number&nbsp;of&nbsp;kernel&nbsp;evaluations.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.options&nbsp;[struct[&nbsp;Copy&nbsp;of&nbsp;input&nbsp;argument&nbsp;options.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Example:</span></span><br>
<span class=help>%&nbsp;&nbsp;data&nbsp;=&nbsp;load('pentagon');</span><br>
<span class=help>%&nbsp;&nbsp;options&nbsp;=&nbsp;struct('ker','rbf','arg',1,'C',1000,'verb',1);</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;oaosvm(&nbsp;data,&nbsp;options&nbsp;);</span><br>
<span class=help>%&nbsp;&nbsp;figure;&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;ppatterns(data);&nbsp;ppatterns(model.sv.X,'ok',13);</span><br>
<span class=help>%&nbsp;&nbsp;pboundary(&nbsp;model&nbsp;);</span><br>
<span class=help>%&nbsp;&nbsp;</span><br>
<span class=help>%&nbsp;See&nbsp;also&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;MVSVMCLASS,&nbsp;OAASVM.</span><br>
<span class=help>%</span><br>
<hr>
<span class=help1>%&nbsp;<span class=help1_field>About:</span>&nbsp;Statistical&nbsp;Pattern&nbsp;Recognition&nbsp;Toolbox</span><br>
<span class=help1>%&nbsp;(C)&nbsp;1999-2005,&nbsp;Written&nbsp;by&nbsp;Vojtech&nbsp;Franc&nbsp;and&nbsp;Vaclav&nbsp;Hlavac</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.cvut.cz"&gt;Czech&nbsp;Technical&nbsp;University&nbsp;Prague&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.feld.cvut.cz"&gt;Faculty&nbsp;of&nbsp;Electrical&nbsp;Engineering&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://cmp.felk.cvut.cz"&gt;Center&nbsp;for&nbsp;Machine&nbsp;Perception&lt;/a&gt;</span><br>
<br>
<span class=help1>%&nbsp;<span class=help1_field>Modifications:</span></span><br>
<span class=help1>%&nbsp;25-jan-2005,&nbsp;VF,&nbsp;option&nbsp;solver&nbsp;replaced&nbsp;by&nbsp;bin_svm&nbsp;</span><br>
<span class=help1>%&nbsp;26-may-2004,&nbsp;VF</span><br>
<span class=help1>%&nbsp;4-feb-2004,&nbsp;VF</span><br>
<span class=help1>%&nbsp;9-Feb-2003,&nbsp;VF</span><br>
<br>
<hr>
<span class=comment>%&nbsp;Process&nbsp;inputs</span><br>
<span class=comment>%-----------------------------</span><br>
<span class=keyword>if</span>&nbsp;<span class=stack>nargin</span>&nbsp;&lt;&nbsp;2,&nbsp;options&nbsp;=&nbsp;[];&nbsp;<span class=keyword>else</span>&nbsp;options=c2s(options);&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'verb'</span>),&nbsp;options.verb&nbsp;=&nbsp;0;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'bin_svm'</span>),&nbsp;options.bin_svm&nbsp;=&nbsp;<span class=quotes>'smo'</span>;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'ker'</span>),&nbsp;options.ker&nbsp;=&nbsp;<span class=quotes>'linear'</span>;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'arg'</span>),&nbsp;options.arg&nbsp;=&nbsp;1;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'C'</span>),&nbsp;options.C&nbsp;=&nbsp;inf;&nbsp;<span class=keyword>end</span><br>
<br>
[dim,num_data]&nbsp;=&nbsp;size(data.X);<br>
nclass&nbsp;=&nbsp;max(data.y);<br>
nrule&nbsp;=&nbsp;(nclass-1)*nclass/2;<br>
<br>
<span class=comment>%&nbsp;display&nbsp;info</span><br>
<span class=comment>%---------------------</span><br>
<span class=keyword>if</span>&nbsp;options.verb&nbsp;==&nbsp;1,<br>
&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>'Binary&nbsp;rules:&nbsp;%d\n'</span>,&nbsp;nrule);<br>
&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>'Training&nbsp;data:&nbsp;%d\n'</span>,&nbsp;num_data);<br>
&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>'Dimension:&nbsp;%d&nbsp;\n'</span>,&nbsp;dim);<br>
&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;isfield(&nbsp;options,&nbsp;<span class=quotes>'ker'</span>),&nbsp;<span class=io>fprintf</span>(<span class=quotes>'Kernel:&nbsp;%s\n'</span>,&nbsp;options.ker);&nbsp;<span class=keyword>end</span><br>
&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;isfield(&nbsp;options,&nbsp;<span class=quotes>'arg'</span>),&nbsp;<span class=io>fprintf</span>(<span class=quotes>'arg:&nbsp;%f\n'</span>,&nbsp;options.arg(1));&nbsp;<span class=keyword>end</span><br>
&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;isfield(&nbsp;options,&nbsp;<span class=quotes>'C'</span>),&nbsp;<span class=io>fprintf</span>(<span class=quotes>'C:&nbsp;%f\n'</span>,&nbsp;options.C);&nbsp;<span class=keyword>end</span><br>
<span class=keyword>end</span><br>
<br>
<span class=comment>%----------------------------------------</span><br>
Alpha&nbsp;=&nbsp;zeros(num_data,nrule);<br>
b&nbsp;=&nbsp;zeros(nrule,1);<br>
bin_y&nbsp;=&nbsp;zeros(2,nrule);<br>
kercnt&nbsp;=&nbsp;0;<br>
<br>
<span class=comment>%&nbsp;One-Against-One&nbsp;decomposition</span><br>
<span class=comment>%-----------------------------------</span><br>
rule&nbsp;=&nbsp;0;<br>
<span class=keyword>for</span>&nbsp;class1&nbsp;=&nbsp;1:nclass-1,<br>
&nbsp;&nbsp;<span class=keyword>for</span>&nbsp;class2&nbsp;=&nbsp;class1+1:nclass,<br>
&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;rule&nbsp;=&nbsp;rule&nbsp;+&nbsp;1;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;options.verb&nbsp;==&nbsp;1,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>'building&nbsp;rule&nbsp;%d-%d&nbsp;(%d&nbsp;of&nbsp;%d)'</span>,&nbsp;...<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;class1,&nbsp;class2,&nbsp;rule,&nbsp;nrule&nbsp;);<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>end</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=comment>%&nbsp;set&nbsp;binary&nbsp;subtask</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=comment>%---------------------------------------------</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;bin_y(1,rule)&nbsp;=&nbsp;class1;<br>
&nbsp;&nbsp;&nbsp;&nbsp;bin_y(2,rule)&nbsp;=&nbsp;class2;<br>
&nbsp;&nbsp;&nbsp;&nbsp;data_inx&nbsp;=&nbsp;find(data.y==class1&nbsp;|&nbsp;data.y==class2);<br>
&nbsp;&nbsp;&nbsp;&nbsp;bin_data.X&nbsp;=&nbsp;data.X(:,&nbsp;data_inx);<br>
&nbsp;&nbsp;&nbsp;&nbsp;bin_data.y&nbsp;=&nbsp;data.y(data_inx);<br>
&nbsp;&nbsp;&nbsp;&nbsp;bin_data.y(find(bin_data.y&nbsp;==&nbsp;class1))&nbsp;=&nbsp;1;<br>
&nbsp;&nbsp;&nbsp;&nbsp;bin_data.y(find(bin_data.y&nbsp;==&nbsp;class2))&nbsp;=&nbsp;2;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=comment>%&nbsp;solve&nbsp;binary&nbsp;subtask</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=comment>%---------------------------------------------</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;bin_model&nbsp;=&nbsp;<span class=eval>feval</span>(&nbsp;options.bin_svm,&nbsp;bin_data,&nbsp;options&nbsp;);<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;Alpha(data_inx(bin_model.sv.inx),rule)&nbsp;=&nbsp;bin_model.Alpha(:);<br>
&nbsp;&nbsp;&nbsp;&nbsp;b(rule)&nbsp;=&nbsp;bin_model.b;<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;kercnt&nbsp;=&nbsp;kercnt&nbsp;+&nbsp;bin_model.kercnt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=comment>%&nbsp;progress&nbsp;info</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=comment>%-----------------------------</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;options.verb&nbsp;==1,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;isfield(bin_model,&nbsp;<span class=quotes>'trnerr'</span>),<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>':&nbsp;trnerr&nbsp;=&nbsp;%.4f'</span>,&nbsp;bin_model.trnerr);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>end</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;isfield(bin_model,&nbsp;<span class=quotes>'margin'</span>),<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>',&nbsp;margin&nbsp;=&nbsp;%f'</span>,&nbsp;bin_model.margin&nbsp;);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>end</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>'\n'</span>);<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>end</span><br>
&nbsp;&nbsp;<br>
&nbsp;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>end</span><br>
<br>
<span class=comment>%&nbsp;set&nbsp;output&nbsp;model</span><br>
<span class=comment>%---------------------------------</span><br>
<br>
<span class=comment>%&nbsp;indices&nbsp;of&nbsp;all&nbsp;support&nbsp;vectors</span><br>
inx&nbsp;=&nbsp;find(sum(abs(Alpha),2)~=&nbsp;0);<br>
<br>
model.Alpha&nbsp;=&nbsp;Alpha(inx,:);<br>
model.b&nbsp;=&nbsp;b;<br>
model.bin_y&nbsp;=&nbsp;bin_y;<br>
model.sv.X&nbsp;=&nbsp;data.X(:,inx);<br>
model.sv.y&nbsp;=&nbsp;data.y(inx);<br>
model.sv.inx&nbsp;=&nbsp;inx;<br>
model.nsv&nbsp;=&nbsp;length(inx);<br>
model.kercnt&nbsp;=&nbsp;kercnt;<br>
model.options&nbsp;=&nbsp;options;<br>
model.fun&nbsp;=&nbsp;<span class=quotes>'mvsvmclass'</span>;<br>
model.trnerr&nbsp;=&nbsp;cerror(&nbsp;mvsvmclass(data.X,&nbsp;model),&nbsp;data.y&nbsp;);<br>
<br>
<span class=comment>%&nbsp;display&nbsp;info</span><br>
<span class=comment>%--------------------</span><br>
<span class=keyword>if</span>&nbsp;options.verb&nbsp;==&nbsp;1,<br>
&nbsp;&nbsp;<span class=io>fprintf</span>(<span class=quotes>'Total&nbsp;training&nbsp;error&nbsp;=&nbsp;%.4f\n'</span>,&nbsp;model.trnerr);<br>
<span class=keyword>end</span><br>
<br>
<span class=jump>return</span>;<br>
<span class=comment>%&nbsp;EOF</span><br>
<br>
<br>
<br>
</code>
